// Checks all possible squares that could attack the king
boolean inCheck(Color color, chessboard, int x, int y)
{
	int i = x+1;
	// Checks spaces above, which can only be hit by queen, and rook(or king within one space)
	while(i < 8)
	{
		Piece piece = chessboard[i][y];
		// if no piece is present, then there isn't a piece there to put the king in check
		if(piece == null)
		{
			i++; 
			continue;
		}
		
		// if the color of the piece is the same as the kings color, then that piece blocks 
		// all attacks from that direction, thus it can break out of the loop
		if(piece.color == color)
			break;
		
		if(piece.name == "ROOK" || piece.name == "QUEEN" || (piece.name == "KING" && i == x+1))
			return true;
		
		i++;
	}
	
	// Checks below the king
	i = x-1;
	while(i >= 0)
	{
		Piece piece = chessboard[i][y];
		// if no piece is present, then there isn't a piece there to put the king in check
		if(piece == null)
		{
			i--; 
			continue;
		}
		
		// if the color of the piece is the same as the kings color, then that piece blocks 
		// all attacks from that direction, thus it can break out of the loop
		if(piece.color == color)
			break;
		
		if(piece.name == "ROOK" || piece.name == "QUEEN" || (piece.name == "KING" && i == x-1))
			return true;
		i--;
	}
	
	// Checks left of the king
	i = y-1;
	while(i >= 0)
	{
		Piece piece = chessboard[x][i];
		// if no piece is present, then there isn't a piece there to put the king in check
		if(piece == null)
		{
			i--; 
			continue;
		}
		
		// if the color of the piece is the same as the kings color, then that piece blocks 
		// all attacks from that direction, thus it can break out of the loop
		if(piece.color == color)
			break;
		
		if(piece.name == "ROOK" || piece.name == "QUEEN" || (piece.name == "KING" && i == y-1))
			return true;
		i--;
	}
		
	// Checks right of the king
	i = y+1;
	while(i < 8)
	{
		Piece piece = chessboard[x][i];
		// if no piece is present, then there isn't a piece there to put the king in check
		if(piece == null)
		{
			i++; 
			continue;
		}
		
		// if the color of the piece is the same as the kings color, then that piece blocks 
		// all attacks from that direction, thus it can break out of the loop
		if(piece.color == color)
			break;
			
		if(piece.name == "ROOK" || piece.name == "QUEEN" || (piece.name == "KING" && i == y+1))
			return true;
		i++;
	}
	
	// Now check the diagonals for enemy bishops and queens
	// Also kings and pawns within one
	// TODO pawns, cuz I dunno which color is on top and which is on bottom
	i = x+1;
	int j = y+1;
	while(i < 8 && j < 8)
	{
		Piece piece = chessboard[i][j];
		// if no piece is present, then there isn't a piece there to put the king in check
		if(piece == null)
		{
			i++; 
			j++;
			continue;
		}
		
		// if the color of the piece is the same as the kings color, then that piece blocks 
		// all attacks from that direction, thus it can break out of the loop
		if(piece.color == color)
			break;
			
		if(piece.name == "BISHOP" || piece.name == "QUEEN" || (piece.name == "KING" && i == x+1))
			return true;
		i++;
		j++;
	}
	
	i = x+1;
	int j = y-1;
	while(i < 8 && j >= 0)
	{
		Piece piece = chessboard[i][j];
		// if no piece is present, then there isn't a piece there to put the king in check
		if(piece == null)
		{
			i++; 
			j--;
			continue;
		}
		
		// if the color of the piece is the same as the kings color, then that piece blocks 
		// all attacks from that direction, thus it can break out of the loop
		if(piece.color == color)
			break;
			
		if(piece.name == "BISHOP" || piece.name == "QUEEN" || (piece.name == "KING" && i == x+1))
			return true;
		i++;
		j--;
	}
	
	i = x-1;
	int j = y-1;
	while(i >= 0 && j >= 0)
	{
		Piece piece = chessboard[i][j];
		// if no piece is present, then there isn't a piece there to put the king in check
		if(piece == null)
		{
			i--; 
			j--;
			continue;
		}
		
		// if the color of the piece is the same as the kings color, then that piece blocks 
		// all attacks from that direction, thus it can break out of the loop
		if(piece.color == color)
			break;
			
		if(piece.name == "BISHOP" || piece.name == "QUEEN" || (piece.name == "KING" && i == x-1))
			return true;
		i--;
		j--;
	}
	
	i = x-1;
	int j = y+1;
	while(i >= 0 && j < 8)
	{
		Piece piece = chessboard[i][j];
		// if no piece is present, then there isn't a piece there to put the king in check
		if(piece == null)
		{
			i--; 
			j++;
			continue;
		}
		
		// if the color of the piece is the same as the kings color, then that piece blocks 
		// all attacks from that direction, thus it can break out of the loop
		if(piece.color == color)
			break;
			
		if(piece.name == "BISHOP" || piece.name == "QUEEN" || (piece.name == "KING" && i == x-1))
			return true;
		i--;
		j++;
	}
	
	// Now check for knights
	Piece piece = chessboard[x+1][y+2];
	if(piece.color != color && piece.name == "KNIGHT")
		return true;
	
	Piece piece = chessboard[x+1][y-2];
	if(piece.color != color && piece.name == "KNIGHT")
		return true;
	
	Piece piece = chessboard[x-1][y+2];
	if(piece.color != color && piece.name == "KNIGHT")
		return true;
	
	Piece piece = chessboard[x-1][y-2];
	if(piece.color != color && piece.name == "KNIGHT")
		return true;
	
	Piece piece = chessboard[x+2][y+1];
	if(piece.color != color && piece.name == "KNIGHT")
		return true;
	
	Piece piece = chessboard[x+2][y-1];
	if(piece.color != color && piece.name == "KNIGHT")
		return true;
	
	Piece piece = chessboard[x-2][y+1];
	if(piece.color != color && piece.name == "KNIGHT")
		return true;
	
	Piece piece = chessboard[x-2][y-1];
	if(piece.color != color && piece.name == "KNIGHT")
		return true;
	
	// Not in check, so return false
	return false;
}

enum status
{
	CHECKMATE_WHITE,
	CHECKMATE_BLACK,
	CHECK_WHITE,
	CHECK_BLACK,
	STALEMATE,
	NORMAL;	
}

status checkMate(color color, chessboard, int x, int y)
{
	// Problems: doesn't account for blocking check with another piece.  Doesn't check if all these are legal moves.  
	// Stalemate needs to check if other pieces are in check.
	if(inCheck(color, chessboard, x+1, y+1) && inCheck(color, chessboard, x+1, y) && inCheck(color, chessboard, x, y+1) 
			&& inCheck(color, chessboard, x+1, y-1) && inCheck(color, chessboard, x-1, y+1) 
			&& inCheck(color, chessboard, x-1, y-1) && inCheck(color, chessboard, x-1, y) && inCheck(color, chessboard, x, y-1))
	{
		if(inCheck(color, chessboard, x, y))
		{
			if(color == BLACK)
				return CHECKMATE_BLACK;
			else
				return CHECKMATE_WHITE;
		}
		else
			return STALEMATE;
	}
	else
	{
		if(inCheck(color, chessboard, x, y))
		{
			if(color == BLACK)
				return CHECK_BLACK;
			else
				return CHECK_WHITE;
		}
		else
			return NORMAL;
	}
}